<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card>
      <div class="add-roles">
        <el-button type="primary" @click="addroles=true">添加角色</el-button>
        <!-- 表格区域 -->
        <el-table :data="roleslist" border="">
          <el-table-column type="expand">
            <template slot-scope="scope">
              <el-row :class="['bbottom', index1 === 0 ? 'btop' : '', 'vcenter']" v-for="(item1,index1) in scope.row.children" :key="item1.id">
                <el-col :span="5">
                  <el-tag closable >{{item1.authName}}</el-tag>
                  <i class="el-icon-caret-right"></i>
                </el-col>
                <el-col :span="19">
                  <el-row :class="[index2 === 0 ? '' : 'btop', 'vcenter']" v-for="(item2,index2) in item1.children" :key="item2.id">
                    <el-col :span="6">
                      <el-tag closable type="warning">{{item2.authName}}</el-tag>
                      <i class="el-icon-caret-right"></i>
                    </el-col>
                    <el-col :span="18">
                      <el-tag @close="delrights(scope.row,item3.id)" closable type="danger" v-for="item3 in item2.children" :key="item3.id">{{item3.authName}}</el-tag>
                      <i class="el-icon-caret-right"></i>
                    </el-col>
                  </el-row>
                </el-col>
              </el-row>
            </template>
          </el-table-column>
          <el-table-column label="#" type="index"></el-table-column>
          <el-table-column label="角色名称" prop="roleName"></el-table-column>
          <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-tooltip class="item" effect="dark" :enterable="false" content="修改角色" placement="top">
                <el-button size="mini" @click="search(scope.row.id)" type="primary" icon="el-icon-edit">编辑</el-button>
              </el-tooltip>
              <el-tooltip content="分配权限" placement="top" :enterable="false" class="item" effect="dark">
                <el-button size="mini" @click="give_rights(scope.row)" type="danger" icon="el-icon-share">分配权限</el-button>
              </el-tooltip>
              <el-tooltip placement="top" :enterable="false" class="item" effect="dark" content="删除角色">
                <el-button size="mini" @click="remove(scope.row.id)" type="warning" icon="el-icon-delete">删除</el-button>
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-card>
    <!-- 添加角色的对话框 -->
    <el-dialog  title="添加角色" :visible.sync="addroles" width="45%" @close="Change_close">
      <el-form :model="rolesfrom" ref="rolesfrom" :rules="roles_rules" label-width="80px">
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="rolesfrom.roleName"></el-input>
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="rolesfrom.roleDesc"></el-input>
        </el-form-item>
      </el-form>
      <!-- 添加用户的表单 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addroles = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 修改角色的对话框 -->
    <el-dialog  title="修改角色" :visible.sync="fixroles" width="45%" @close="Change_close">
      <el-form :model="rolesfrom" ref="rolesfrom" :rules="roles_rules" label-width="80px">
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="rolesfrom.roleName"></el-input>
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="rolesfrom.roleDesc"></el-input>
        </el-form-item>
      </el-form>
      <!-- 添加用户的表单 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="fixroles = false">取 消</el-button>
        <el-button type="primary" @click="submitfixroles">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 分配权限 -->
    <el-dialog  title="分配权限" :visible.sync="giverights" width="45%" @close="Change_rights">
      <!-- 添加用户的表单 -->
      <!-- 树形控件 -->
      <el-tree
      :data="rightlist"
      :props="treeprops" show-checkbox node-key="id" default-expand-all
      :default-checked-keys="defaultarr"
       ref="treename"></el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="giverights = false">取 消</el-button>
        <el-button type="primary" @click="giveItem">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {getroles,addroles,delroles,searchroles,setfix,removerights,getrights,sentrights} from "@/api/login"
export default {
  data(){
    return{
      roleslist:[],
      // 权限列表
      rightlist:[],
      // 添加角色
      addroles:false,
      // 修改角色
      fixroles:false,
      // 分配权限
      giverights:false,
      // 角色内容
      rolesfrom:{
        roleName:"",
        roleDesc:""
      },
      // 选中角色的id
      id:"",
      // 角色表单规则
      roles_rules:{
        roleName: [
          { required: true, message: '请输入角色名称', trigger: 'blur' },
          { min: 1, max: 5, message: '长度在 1 到 5 个字符', trigger: 'blur' }
        ]
      },
      // tree控件
      treeprops:{
        children: 'children',
        label: 'authName'
      },
      // 默认被选中的权限
      defaultarr:[]
    }
  },
  created(){
    this.load_data()
  },
  methods:{
    load_data(){
      getroles().then(res=>{
        console.log(res)
        this.roleslist=res.data.data
      })
    },
    Change_close(){
      this.$refs.rolesfrom.resetFields();
    },
    // 表单验证和添加角色
    submitForm(){
      const{rolesfrom}=this
       this.$refs.rolesfrom.validate((valid)=>{
         console.log(valid)
         if(valid){
           addroles({roleName:rolesfrom.roleName,roleDesc:rolesfrom.roleDesc}).then(res=>{
             console.log(res)
             this.load_data()
             this.addroles=false
           })
         }
       })
    },
    // 删除角色
    async remove(id){
      const res = await this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
      }).catch(err => err)
      if(res==="confirm"){
        delroles("roles/"+id).then(res=>{
          console.log(res)
          this.$message.success(res.data.meta.msg);
          this.load_data()
        })
      }
    },
    // 根据 ID 查询角色
    async search(id){
      const res = await searchroles("roles/"+id)
      console.log(res)
      this.rolesfrom.roleName=res.data.data.roleName
      this.rolesfrom.roleDesc=res.data.data.roleDesc
      this.id=res.data.data.roleId
      this.fixroles=!this.fixroles
    },
    // 提交修改跟表单验证
    submitfixroles(){
      const{rolesfrom,id}=this
      this.$refs.rolesfrom.validate(valid=>{
        // console.log(valid)
        if(valid){
          setfix({roleName:rolesfrom.roleName,roleDesc:rolesfrom.roleDesc},"roles/"+id).then(res=>{
            console.log(res)
            this.load_data()
            this.fixroles=false
          })
        }
      })
    },
    // 移除权限
    async delrights(role,rightId){
      console.log(role,rightId)
      const res = await this.$confirm('此操作将永久删除该权限, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      console.log(res)
      if(res==="confirm"){
        const res = await removerights('roles/${role.id}/rights/${rightId}')
        console.log(res)
      }
    },
    // 点击分配权限 ，获取树形权限
    give_rights(role){
      console.log(role)
      this.giverights=!this.giverights
      this.id=role.id
      getrights('rights/tree').then(res=>{
        console.log(res)
        this.rightlist=res.data.data
      })
      this.selectitem(role,this.defaultarr)
      // console.log(this.defaultarr)
    },
    // 筛选权限
    selectitem(data,Arry){
      if(!data.children){
       return Arry.push(data.id)
      }
      data.children.forEach(item => {
        this.selectitem(item,Arry)
      });
      // console.log(this.defaultarr)
    },
    // 关闭权限对话框，并重置选中的权限数组
    Change_rights(){
      this.defaultarr=[]
      this.giverights=false
    },
    // 分配权限
    giveItem(){
      this.giverights=false
      var arr=[
        ...this.$refs.treename.getCheckedKeys(),
        ...this.$refs.treename.getHalfCheckedKeys(),
      ]
      var arrstr=arr.join(',')
      console.log(arrstr)
      sentrights('roles/'+this.id+'/rights',{rids:arrstr}).then(res=>{
        console.log(res)
        this.load_data()
      })
    }
  }
}
</script>
<style Lang="less" scoped>
.el-tag {
  margin: 8px;
}
.btop {
  border-top: 1px solid #eee;
}
.bbottom {
  border-bottom: 1px solid #eee;
}
.vcenter {
  display: flex;
  align-items: center;
}
</style>